package com.lanju.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanju.common.SpringUtils;
import com.lanju.entity.Agreement;
import com.lanju.entity.User;
import com.lanju.service.AgreementService;
import com.lanju.service.ServiceService;

@Controller
@RequestMapping("/service")
public class ServiceController {
	private static final Logger LOGGER = LoggerFactory
            .getLogger(ServiceController.class);
    @Autowired
    ServiceService serviceService;
    @Autowired AgreementService agreementService;
    
//    @Resource
//   	JedisService jedisService;
    
    @RequestMapping("/queryServiceList")
    @ResponseBody
    public Map<String, Object> queryServiceList(HttpServletRequest request){
    	Map<String, Object> param = SpringUtils.getParameterMap(request);
    	Integer pageSize = Integer.parseInt( (String)param.get("pageSize") );
		Integer pageNumber = Integer.parseInt( (String)param.get("pageNumber") );
		Integer offset = (pageNumber-1)*pageSize;
        return serviceService.queryServiceList(param, pageSize, offset);
        
    }
    
    @RequestMapping("/queryServiceDetailById")
    @ResponseBody
    public Map<String, Object> queryServiceDetailById(HttpServletRequest request){
    	Map<String, Object> param = SpringUtils.getParameterMap(request);
        return serviceService.queryServiceDetailById(param);
        
    }
    
    @RequestMapping("/queryUserSeerviceRel")
    @ResponseBody
    public Map<String, Object> queryUserSeerviceRel(HttpServletRequest request,HttpSession session){
    	Map<String, Object> param = SpringUtils.getParameterMap(request);
    	User user =(User) session.getAttribute("userModel");
		param.put("creator", user.getUserId());
    	//param.put("creator", "92732017-59d8-4572-8dcd-65c827779a4d");
        return serviceService.queryUserSeerviceRel(param);
    }
    
    @RequestMapping("/insertUserSeerviceRel")
    @ResponseBody
    public Map<String, Object> insertUserServiceOrderRel(HttpServletRequest request,HttpServletResponse response, HttpSession session){
    	Map<String, Object> param = SpringUtils.getParameterMap(request);
    	User user =(User) session.getAttribute("userModel");
    	param.put("creator", user.getUserId());
    	param.put("type", user.getFlag());
    	param.put("phone", user.getPhone());
    	//param.put("type", 1);
    	//param.put("creator", "92732017-59d8-4572-8dcd-65c827779a4d");
        return serviceService.insertUserServiceOrderRel(param,request,response);
    }
    
    @RequestMapping("/queryPackageList")
    @ResponseBody
    //http://localhost:8080/LanJuPublic/jsp/service/serviceList.html?serviceType=1
    public Map<String, Object> queryPackageList(HttpServletRequest request,HttpSession session){
    	Map<String, Object> param;
		try {
			param = SpringUtils.getParameterMap(request);
			if(StringUtils.isEmpty(param.get("userId"))){
				User user =(User) session.getAttribute("userModel");
				if(user !=null){
					if(user.getFlag()!=2){
						param.put("userId", user.getUserId());
					}else{
						param.put("phone", param.get("searchText").toString());
						param.put("name", param.get("searchText").toString());
					}
					return serviceService.queryPackageList(param);
				}else{
					param.put("msg", "您还没登录，请登录再操作吧");
					param.put("rows", null);
					return param;
				}
			}else{
//				param.put("phone", param.get("searchText")==""?null:param.get("searchText").toString());
//				param.put("name", param.get("searchText")==""?null:param.get("searchText").toString());
				return serviceService.queryPackageList(param);
			}
			

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
    }
    
    @RequestMapping("/queryAreaByCity")
    @ResponseBody
    public Map<String, Object> queryAreaByCity(HttpServletRequest request){
    	Map<String, Object> param = SpringUtils.getParameterMap(request);
        return serviceService.queryAreaByCity(param);
    }
    
    @RequestMapping("/queryCommunityByArea")
    @ResponseBody
    public Map<String, Object> queryCommunityByArea(HttpServletRequest request){
    	Map<String, Object> param = SpringUtils.getParameterMap(request);
        return serviceService.queryCommunityByArea(param);
    }
   
    @RequestMapping("/queryPackageDetail")
    @ResponseBody
    public Map<String, Object> queryPackageDetail(HttpServletRequest request){
    	Map<String, Object> param = SpringUtils.getParameterMap(request);
        return serviceService.queryPackageDetail(param);
    }
    /**
     * 修改支付状态
     * @param request
     * @return
     */
    @RequestMapping("/updateStatus")
    @ResponseBody
    public Map<String,Object> updateStatus(HttpServletRequest request,HttpServletResponse response){
    	Map<String, Object> param = SpringUtils.getParameterMap(request);
		return serviceService.updateStatus(param,request,response);
    }
    /**
     * 修改支付状态
     * @param request
     * @return
     */
    @RequestMapping("/updateStatusNoMsg")
    @ResponseBody
    public Map<String,Object> updateStatusNoMsg(HttpServletRequest request,HttpServletResponse response){
    	Map<String, Object> param = SpringUtils.getParameterMap(request);
		return serviceService.updateStatusNoMsg(param,request,response);
    }
    
    @RequestMapping("/insertAppointTime")
    @ResponseBody
    public Map<String, Object> insertAppointTime(HttpServletRequest request){
    	Map<String, Object> param = SpringUtils.getParameterMap(request);
        return serviceService.insertAppointTime(param);
    }
    
    @RequestMapping("/insertCollect")
    @ResponseBody
    public Map<String, Object> insertCollect(HttpServletRequest request,HttpSession session){
    	Map<String, Object> param = SpringUtils.getParameterMap(request);
    	User user =(User) session.getAttribute("userModel");
		param.put("userId", user.getUserId());
        return serviceService.insertCollect(param);
    }
    
    @RequestMapping("/deleteCollect")
    @ResponseBody
    public Map<String, Object> deleteCollect(HttpServletRequest request,HttpSession session){
    	Map<String, Object> param = SpringUtils.getParameterMap(request);
    	User user =(User) session.getAttribute("userModel");
		param.put("userId", user.getUserId());
        return serviceService.deleteCollect(param);
    }
    @RequestMapping("/queryUserMsg")
    @ResponseBody
    public Map<String, Object> queryUserMsg(HttpServletRequest request,HttpSession session){
    	Map<String, Object> param = SpringUtils.getParameterMap(request);
    	User user =(User) session.getAttribute("userModel");
    	if(user == null) {
    		param.put("result", false);
    	}else {
    		param.put("signUserId", user.getUserId());
    		param.put("chargeUserId", user.getUserId());
    		List<Agreement> list=agreementService.getAgreementList(param);
    		if(list !=null &&  list.size()>0){
    			param.put("res", true);
    		}else{
    			param.put("res", false);
    		}
    	}
        return param;
    }
    
    
}
